package com.ssm.blog.controller;

import com.ssm.blog.pojo.Category;
import com.ssm.blog.service.CategoryService;
import com.ssm.blog.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/category.html")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //category.html?act=edit
    @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map<String, Object> edit(Category category) {
        if (category == null) {
            return JsonUtil.getErrorMessage("请输入分类名", null);
        }
        if (category.getId() == null) {
            categoryService.addCategory(category);
        } else {
            categoryService.updateCategory(category);
        }
        return JsonUtil.getSuccessMessage("编辑成功", category);
    }

    @RequestMapping(params = "act=delete")
    @ResponseBody
    public Map<String, Object> delete(int id) {
        categoryService.deleteCategory(id);
        return JsonUtil.getSuccessMessage("删除成功", null);
    }

    @RequestMapping(params = "act=list")
    @ResponseBody
    public List<Category> list(Category category) {
        List<Category> categories = categoryService.getCategoryList(category);
        return categories;
    }
}
